//
//  VNCServerListView.m
//  vnsea
//
//  Created by Chris Reed on 9/9/07.
//  Copyright 2007 __MyCompanyName__. All rights reserved.
//

#import "VNCServerListView.h"
#import <UIKit/UITable.h>
#import <UIKit/UITableCell.h>
#import <UIKit/UISimpleTableCell.h>
#import <UIKit/UITableColumn.h>
#import <UIKit/UIImageAndTextTableCell.h>
#import <UIKit/UINavBarButton.h>
#import <UIKit/UINavigationItem.h>
#import "ServerStandAlone.h"
#import "ServerFromPrefs.h"
#import "VnseaApp.h"
#import "VNCPreferences.h"
#import <Foundation/NSLocale.h>

#define kNavBarHeight (48)
#define kButtonBarHeight (48)

#define kAddButtonWidth (34)
#define kAddButtonHeight (40)

#define kAboutButtonHeight (32)
#define kAboutButtonWidth (60)

#define kDonateButtonHeight (32)
#define kDonateButtonWidth (80)

#define kPreferencesButtonHeight (32)
#define kPreferencesButtonWidth (120)

extern id UIImageGetNavigationBarAddButton();

@implementation VNCServerListView

- (id)initWithFrame:(CGRect)frame
{
	if ([super initWithFrame:frame])
	{
		frame.size.height -= 20;
		frame.origin.y += 20;
		CGRect subframe;
		CGSize navBarSize = [UINavigationBar defaultSize];
		
		subframe = CGRectMake(0.0f, 20.0f, frame.size.width, navBarSize.height);
		
		// Setup navbar
		_navBar = [[UINavigationBar alloc] initWithFrame: subframe];
		[_navBar showLeftButton:nil withStyle:0 rightButton:UIImageGetNavigationBarAddButton() withStyle:0];
		[_navBar setBarStyle: 3];
		[_navBar setDelegate: self];
		[_navBar enableAnimation];	
		
		UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:NSLocalizedString(@"VNC Servers", nil)];
		[_navBar pushNavigationItem:item];

		// Setup button bar at bottom
		subframe = CGRectMake(0, 20 + frame.size.height - kButtonBarHeight, frame.size.width, kButtonBarHeight);
		_buttonBar = [[UIGradientBar alloc] initWithFrame:subframe];
		
        const float kTextComponents[] = { .94, .94, .94, .7 };
        const float kTransparentComponents[] = { 0, 0, 1, 0 };
        const float kTextComponentsDateTime[] = { 0.4, 0.4, 0.4, 1 };
		
        CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
        CGColorRef textColorStatus = CGColorCreate(rgbSpace, kTextComponents);
        CGColorRef rgbTransparent = CGColorCreate(rgbSpace, kTransparentComponents);
		
        _textColorDateTime = CGColorCreate(rgbSpace, kTextComponentsDateTime);
		CGColorSpaceRelease(rgbSpace);
		
		// Go ahead and create font for drawing the last connect column
		_lastConnectFont = GSFontCreateWithName("Helvetica", 0, 12.0f);
		
		// Add about button
		subframe = CGRectMake(10.0f, (kButtonBarHeight - kAboutButtonHeight) / 2.0 + 1, kAboutButtonWidth, kAboutButtonHeight);
		_aboutButton = [[UINavBarButton alloc] initWithFrame:subframe];
		[_aboutButton setAutosizesToFit: NO];
		[_aboutButton addTarget:self action: @selector(showAbout:) forEvents:kGSEventTypeButtonSelected];
		[_aboutButton setNavBarButtonStyle: 0];
		[_aboutButton setTitle: NSLocalizedString(@"Web", nil)];
		[_aboutButton setEnabled: YES];

			subframe = CGRectMake(10.0f + kAboutButtonWidth + 5.0f,  (kButtonBarHeight - kAboutButtonHeight) / 2.0 + 1, kDonateButtonWidth, 32.0f);
			_donateButton = [[UINavBarButton alloc] initWithTitle:NSLocalizedString([[VNCPreferences sharedPreferences] registered] ? @"About" : @"Activate", nil) autosizesToFit:NO];
			[_donateButton setFrame:subframe];
			[_donateButton setNavBarButtonStyle:0];
			[_donateButton addTarget:self action:@selector(donateTarget:) forEvents:kGSEventTypeButtonSelected];

			subframe = CGRectMake(10.0f + kAboutButtonWidth + 5.0f + kDonateButtonWidth + 5.0f,  (kButtonBarHeight - kAboutButtonHeight) / 2.0 + 1, 28.0f, 32.0f);
			_helpButton = [[UINavBarButton alloc] initWithTitle:@"?" autosizesToFit:NO];
			[_helpButton setTitleFont:GSFontCreateWithName("HelveticaBold", 0, 19.0f)];
			[_helpButton setFrame:subframe];
			[_helpButton setNavBarButtonStyle:0];
			[_helpButton addTarget:self action:@selector(getHelp:) forEvents:kGSEventTypeButtonSelected];

		// Add server button
		subframe = CGRectMake(frame.size.width - 10.0f - kPreferencesButtonWidth + 3, (kButtonBarHeight - kPreferencesButtonHeight) / 2.0 + 1, kPreferencesButtonWidth - 3, kPreferencesButtonHeight);
		_preferencesButton = [[UINavBarButton alloc] initWithFrame:subframe];
		[_preferencesButton setAutosizesToFit: NO];
		[_preferencesButton addTarget:self action: @selector(showPreferences:) forEvents:kGSEventTypeButtonSelected];
		[_preferencesButton setNavBarButtonStyle: 0];
		[_preferencesButton setTitle: NSLocalizedString(@"Preferences", nil)];
		[_preferencesButton setEnabled: YES];

		// Setup server table
		subframe = CGRectMake(0, navBarSize.height + 20, frame.size.width, frame.size.height - navBarSize.height - kButtonBarHeight-1);
		_serverColumn = [[UITableColumn alloc] initWithTitle:@"Servers" identifier:@"servers" width:frame.size.width - (150)];
		_serverLastConnectColumn = [[UITableColumn alloc] initWithTitle:@"Last Connect" identifier:@"lastConnect" width:150];
		_serverTable = [[UITable alloc] initWithFrame:subframe];
		[_serverTable addTableColumn:_serverColumn];
		[_serverTable addTableColumn:_serverLastConnectColumn];
		[_serverTable setDelegate:self];
		[_serverTable setDataSource:self];
		[_serverTable setReusesTableCells:NO];
		
		// Construct view hierarchy
		[_navBar addSubview:_addButton];
		[_buttonBar addSubview:_aboutButton];
		[_buttonBar addSubview:_donateButton];
		[_buttonBar addSubview:_helpButton];
		[_buttonBar addSubview:_preferencesButton];
		[self addSubview:_buttonBar];
		[self addSubview:_navBar];
		[self addSubview:_serverTable];

		//[NSThread detachNewThreadSelector:@selector(checkForNewVersion:) toTarget:self withObject:nil];
	}

	return self;
}

- (void)dealloc
{
	CFRelease(_textColorDateTime);
	CFRelease(_lastConnectFont);
	[_servers release];
	[super dealloc];
}

- (void)setServerList:(NSArray *)list
{
	[_servers release];
	_servers = [list retain];
	
	[_serverTable reloadData];
	[_serverTable updateDisclosures];
}

- (void)setDelegate:(id)newDelegate
{
	_delegate = newDelegate;
}

- (id)delegate
{
	return _delegate;
}

- (void)handleNotification:(NSNotification *)notification
{
	NSLog(@"notification:%@ userInfo=%@", notification, [notification userInfo]);
}

- (void)addNewServer:(id)sender
{
	if (_delegate && [_delegate respondsToSelector:@selector(addNewServer)])
	{
		[_delegate addNewServer];
	}
}

- (void)showPreferences:(id)sender
{
	if (_delegate && [_delegate respondsToSelector:@selector(displayPrefs)])
	{
		[_delegate displayPrefs];
	}
}

- (void)showAbout:(id)sender
{
	_aboutPopup = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0,0,350,100) ];	
	[_aboutPopup setTitle:NSLocalizedString(@"Touchpad Pro", nil)];
	[_aboutPopup setBodyText:NSLocalizedString(@"Visit Website", nil)];
	[_aboutPopup setDelegate:self];
	[_aboutPopup setContext:self];
	[_aboutPopup setAlpha:0.6];
	[_aboutPopup setDimsBackground:YES];
	UIPushButton *pb = [_aboutPopup addButtonWithTitle:NSLocalizedString(@"Take Me There", nil)];
	CGRect frame = [pb frame];
	frame.size.width /= 2;
	[pb setFrame: frame];
	pb = [_aboutPopup addButtonWithTitle:NSLocalizedString(@"NoMaybeLater", nil)];
	frame = [pb frame];
	frame.size.width /= 2;
	frame.origin.y -= frame.size.height;
	frame.origin.x += frame.size.width;
	[pb setFrame: frame];

	[_aboutPopup setTableShouldShowMinimumContent:NO];
	[_aboutPopup setBlocksInteraction:YES];

//	[_aboutPopup _slideSheetOut:YES];
	[_aboutPopup layoutAnimated:NO];
//	[_aboutPopup presentSheetFromAboveView:self];
	[_aboutPopup popupAlertAnimated:YES atOffset:0.0];
}

- (void) alertSheet:(id)sheet buttonClicked:(int)button
{
	if (sheet == _aboutPopup)
	{
		[sheet dismissAnimated:YES];
//		[sheet release];
		if (button == 1)
			[_delegate openURL: [NSURL URLWithString: @"http://www.touchpadpro.com/"]];
	}
	else if (sheet == _donatePopup)
	{
		[sheet dismissAnimated:YES];
	}
	else if (sheet == _activatePopup)
	{
		[sheet dismissAnimated:YES];
		if (button == 1)
			[_delegate openURL: [NSURL URLWithString: @"http://www.touchpadpro.com/2008/04/touchpad-pro-screenview-activation.html"]];
		if (button == 2)
			[_delegate openURL: [NSURL URLWithString: [NSString stringWithFormat: @"http://x.touchpadpro.com/buy.php?custom=%@", [_delegate serial]]]];
		else if (button == 3)
			[self tryToActivateUsingLicenseKey:NO];
	}
	else if (sheet == _activateDetailsPopup)
	{
		if (button==1)
			[self getActivateDeviceDetails];
		else if (button==2)
			[self getActivateComputerDetails];
		else if (button==3)
			[_delegate openURL: [NSURL URLWithString: @"http://www.touchpadpro.com/2008/04/touchpad-pro-screenview-activation.html"]];
		[sheet dismissAnimated:YES];
	}
	else if (sheet == _activateDetailsComputerPopup)
	{
		[sheet dismissAnimated:YES];
//		[sheet release];
//		if (button == 1)
//			[self tryToActivateUsingLicenseKey:YES];
	}
	else if (sheet == _activateDetailsDevicePopup)
	{
		[sheet dismissAnimated:YES];
//		[sheet release];
		if (button == 1)
			[_delegate openURL: [NSURL URLWithString: [NSString stringWithFormat: @"http://x.touchpadpro.com/buy.php?custom=%@", [_delegate serial]]]];
	}
	else
	{
		[sheet dismissAnimated:YES];
//		[sheet release];
	}
}

- (void)tryToActivateUsingLicenseKey:(bool)isUsingLicenseKey
{
//	if ([[[VNCPreferences sharedPreferences] licenseKey] length]>0)
	{
		[self showRegConnect: YES];
		_checkingForRegistration = YES;
		[NSThread detachNewThreadSelector:@selector(checkForRegistration:) toTarget:self withObject:nil];
		while (_checkingForRegistration)
			[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
		[self showRegConnect: NO];
		if (_registration!=nil && [_registration length] > 0)
		{
			if ([_registration characterAtIndex: 0] == '1')
			{
				[[VNCPreferences sharedPreferences] setRegistered: YES];
				[_donateButton setTitle: NSLocalizedString(@"About", nil)];
			}
		}
		[self activationMessage: [[VNCPreferences sharedPreferences] registered] usingLicenseKey: isUsingLicenseKey];
		if (_registration)
			[_registration release];
	}
//	else
//		[self activationMessage:NO];
}

- (void)checkForRegistration:(id)blah
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSString *theurl = [NSString stringWithFormat: @"http://x.touchpadpro.com/check.php?v=4.11&serial=%@", [_delegate serial]];
	if ([theurl characterAtIndex: 0] != 'h' || [theurl characterAtIndex: 1] != 't' || [theurl characterAtIndex: 2] != 't' || [theurl characterAtIndex: 3] != 'p' || [theurl characterAtIndex: 4] != ':' || [theurl characterAtIndex: 5] != '/' || [theurl characterAtIndex: 6] != '/' || 
		[theurl characterAtIndex: 7] != 'x' || [theurl characterAtIndex: 8] != '.' ||
		[theurl characterAtIndex: 9] != 't' || [theurl characterAtIndex: 10] != 'o' || [theurl characterAtIndex: 11] != 'u' || [theurl characterAtIndex: 12] != 'c' || [theurl characterAtIndex: 13] != 'h' || [theurl characterAtIndex: 14] != 'p' || [theurl characterAtIndex: 15] != 'a' || [theurl characterAtIndex: 16] != 'd' || [theurl characterAtIndex: 17] != 'p' || [theurl characterAtIndex: 18] != 'r' || [theurl characterAtIndex: 19] != 'o' || [theurl characterAtIndex: 20] != '.' || 
		[theurl characterAtIndex: 21] != 'c' || [theurl characterAtIndex: 22] != 'o' || [theurl characterAtIndex: 23] != 'm' || [theurl characterAtIndex: 24] != '/')
		[_delegate terminate];
	_registration = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString: theurl]];
	_checkingForRegistration = NO;
	[pool release];
}

- (void)checkForNewVersion:(id)blah
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	NSString *newVersion = [[NSString stringWithContentsOfURL: [NSURL URLWithString: [NSString stringWithFormat: @"http://check.touchpadpro.com/4.0/newversion.html?sn=%@&rg=%d&tms=%d&mac=%d&win=%d&svon=%d&svoff=%d&svhd=%d&lo=%d&hi=%d,tsv=%d", [_delegate serial], [_delegate registered] ? 1 : 0, [[VNCPreferences sharedPreferences] tmsCount], [[VNCPreferences sharedPreferences] macCount], [[VNCPreferences sharedPreferences] winCount], [[VNCPreferences sharedPreferences] svOnCount], [[VNCPreferences sharedPreferences] svOffCount], [[VNCPreferences sharedPreferences] svHDCount], [[VNCPreferences sharedPreferences] loResCount], [[VNCPreferences sharedPreferences] hiResCount], [[VNCPreferences sharedPreferences] trySvCount]]]] lowercaseString];
	if (newVersion != nil && [newVersion length] > 0)
	{
		NSRange start = [newVersion rangeOfString: @"<body>"];
		NSRange stop = [newVersion rangeOfString: @"</body>"];
		if (start.length > 0 && stop.length > 0)
		{
//			NSLog(@"start loc %d len %d, stop loc %d len %d", start.location, start.length, stop.location, stop.length);
			NSString *newwebversion = [newVersion substringWithRange: NSMakeRange(start.location + start.length, stop.location - start.location - start.length)];
//			NSLog(@"newver %d", [newwebversion intValue]);
			[[VNCPreferences sharedPreferences] setLastWebVersion: [newwebversion intValue]];
		}
	}
	[pool release];
}

- (void)activationMessage:(bool)success usingLicenseKey:(bool)isUsingLicenseKey
{
	NSString *title, *body, *license = [[VNCPreferences sharedPreferences] licenseKey];
	if (success)
	{
		title = NSLocalizedString(@"ThanksTitle", nil);
		body = NSLocalizedString(@"ThanksMessage", nil);
	}
	else
	{
		title = NSLocalizedString(@"ErrorTitle", nil);
		if (isUsingLicenseKey)
		{
			if ([license length]>0)
				body = [NSString stringWithFormat: NSLocalizedString(@"ActivationErrorMessage", nil), license, [_delegate serial]];
			else
				body = [NSString stringWithFormat: NSLocalizedString(@"ActivationErrorMessageNoKey", nil), license, [_delegate serial]];
		}
		else
		{
			if (_registration == nil || [_registration length] == 0)
				body = NSLocalizedString(@"ActivationErrorMessageNoNet", nil);
			else
				body = [NSString stringWithFormat: NSLocalizedString(@"ActivationErrorMessageNoReg", nil), [_delegate serial]];
		}
	}
	
	UIAlertSheet * hotSheet = [[UIAlertSheet alloc]
		initWithTitle:title
		buttons:[NSArray arrayWithObject:NSLocalizedString(@"OK", nil)]
		defaultButtonIndex:0
		delegate:self
		context:self];

	[hotSheet setBodyText:body];
	[hotSheet setDimsBackground:YES];
	[hotSheet setRunsModal:YES];
	[hotSheet setShowsOverSpringBoardAlerts:NO];
	[hotSheet popupAlertAnimated:YES];	
}

- (void)showRegConnect:(bool)isConnect
{
	if (isConnect)
	{
		_progressHUD = [[UIProgressHUD alloc] initWithWindow: self];
		[_progressHUD setBackgroundColor:GSColorCreateColorWithDeviceRGBA(1,1,1, 0.6)];
		[_progressHUD setText: @"Authenticating..."];
		[_progressHUD show:YES];
	}
	else
	{
		[_progressHUD removeFromSuperview];
		[_progressHUD release];
	}
}

- (void)donateTarget:(id)sender
{
	if ([[VNCPreferences sharedPreferences] registered])
		[self getDonate];
	else
		[self getActivate];
}

- (void)getDonate
{
	_donatePopup = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0,0,350,350) ];	
	[_donatePopup setTitle:NSLocalizedString(@"AboutVersion", nil)];
	UIPushButton *button;
	UITextLabel *textlabel;
	[_donatePopup setDelegate:self];
	[_donatePopup setContext:self];
	[_donatePopup setDimsBackground:YES];
	[_donatePopup addButtonWithTitle:NSLocalizedString(@"OK", nil)];

//	[_donatePopup setTableShouldShowMinimumContent:NO];
	[_donatePopup setBlocksInteraction:YES];

	[_donatePopup _slideSheetOut:YES];
	[_donatePopup layoutAnimated:NO];
	[_donatePopup setBodyText:NSLocalizedString(@"AboutText", nil)];
	[_donatePopup popupAlertAnimated:YES atOffset:0.0];

	[_donatePopup setAlpha:1.0f];
}

- (void)getActivate
{
	_activatePopup = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0,0,350,350) ];	
	[_activatePopup setTitle:NSLocalizedString(@"ActivateTitle", nil)];
	[_activatePopup setDelegate:self];
	[_activatePopup setContext:self];
	[_activatePopup setDimsBackground:YES];
	[_activatePopup addButtonWithTitle:NSLocalizedString(@"Tell Me More", nil)];
	[_activatePopup addButtonWithTitle:NSLocalizedString(@"Buy Activation", nil)];
	[_activatePopup addButtonWithTitle:NSLocalizedString(@"Check Activation", nil)];
	[_activatePopup addButtonWithTitle:NSLocalizedString(@"Activate Later", nil)];
	[_activatePopup setBlocksInteraction:YES];
//	[_activatePopup _slideSheetOut:YES];
	[_activatePopup layoutAnimated:NO];
	[_activatePopup setBodyText:NSLocalizedString(@"Activation Request Text", nil)];
	[_activatePopup popupAlertAnimated:YES atOffset:0.0];
	[_activatePopup setAlpha:1.0f];
}

- (void)getActivateDetails
{
	_activateDetailsPopup = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0,0,350,350) ];	
	[_activateDetailsPopup setTitle:NSLocalizedString(@"ActivateDetailsTitle", nil)];
	[_activateDetailsPopup setDelegate:self];
	[_activateDetailsPopup setContext:self];
	[_activateDetailsPopup setDimsBackground:YES];
	[_activateDetailsPopup addButtonWithTitle:NSLocalizedString(@"ActivateOnDevice", nil)];
	[_activateDetailsPopup addButtonWithTitle:NSLocalizedString(@"ActivateOnComputer", nil)];
	[_activateDetailsPopup addButtonWithTitle:NSLocalizedString(@"ActivationDetailsWanted", nil)];
	[_activateDetailsPopup addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
	[_activateDetailsPopup setBlocksInteraction:YES];
//	[_activateDetailsPopup _slideSheetOut:YES];
	[_activateDetailsPopup layoutAnimated:NO];
	[_activateDetailsPopup setBodyText:NSLocalizedString(@"Activation Details Text", nil)];
	[_activateDetailsPopup popupAlertAnimated:NO atOffset:0.0];
	[_activateDetailsPopup setAlpha:1.0f];
}

- (void)getActivateComputerDetails
{
	_activateDetailsComputerPopup = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0,0,350,350) ];	
	[_activateDetailsComputerPopup setTitle:NSLocalizedString(@"ActivateComputerDetailsTitle", nil)];
	[_activateDetailsComputerPopup setDelegate:self];
	[_activateDetailsComputerPopup setContext:self];
	[_activateDetailsComputerPopup setDimsBackground:YES];
//	[_activateDetailsComputerPopup addButtonWithTitle:NSLocalizedString(@"ActivateWithLicenseKey", nil)];
	[_activateDetailsComputerPopup addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
	[_activateDetailsComputerPopup setBlocksInteraction:YES];
//	[_activateDetailsComputerPopup _slideSheetOut:YES];
	[_activateDetailsComputerPopup layoutAnimated:NO];
	[_activateDetailsComputerPopup setBodyText:NSLocalizedString(@"Activation Computer Details Text", nil)];
	
	if ([[[VNCPreferences sharedPreferences] licenseKey] length]>0)
		[(UIPushButton*)[[_activateDetailsComputerPopup buttons] objectAtIndex:0] setEnabled:NO];
	
	[_activateDetailsComputerPopup popupAlertAnimated:NO atOffset:0.0];
	[_activateDetailsComputerPopup setAlpha:1.0f];
}

- (void)getActivateDeviceDetails
{
	_activateDetailsDevicePopup = [[UIAlertSheet alloc ] initWithFrame:CGRectMake(0,0,350,350) ];	
	[_activateDetailsDevicePopup setTitle:NSLocalizedString(@"ActivateDeviceDetailsTitle", nil)];
	[_activateDetailsDevicePopup setDelegate:self];
	[_activateDetailsDevicePopup setContext:self];
	[_activateDetailsDevicePopup setDimsBackground:YES];
//	[_activateDetailsDevicePopup addButtonWithTitle:NSLocalizedString(@"CheckActivation", nil)];
	[_activateDetailsDevicePopup addButtonWithTitle:NSLocalizedString(@"BuyActivation", nil)];
	[_activateDetailsDevicePopup addButtonWithTitle:NSLocalizedString(@"Cancel", nil)];
	[_activateDetailsDevicePopup setBlocksInteraction:YES];
//	[_activateDetailsDevicePopup _slideSheetOut:YES];
	[_activateDetailsDevicePopup layoutAnimated:NO];
	[_activateDetailsDevicePopup setBodyText:NSLocalizedString(@"Activation Device Details Text", nil)];
	[_activateDetailsDevicePopup popupAlertAnimated:NO atOffset:0.0];
	[_activateDetailsDevicePopup setAlpha:1.0f];
}

- (void)getHelp:(id)sender
{
	[_delegate showWebPage:@"http://direct.touchpadpro.com/4.0/connect.html" withTitle:NSLocalizedString(@"Help",nil) fromView:self];
}

- (void)navigationBar:(id)navBar buttonClicked:(int)buttonIndex
{
	NSLog(@"navbar:%@ button:%d", navBar, buttonIndex);
	switch (buttonIndex)
	{
		case kNavBarAddServerButton:
			[self addNewServer:nil];
			break;
	}
}

- (void)tableRowSelected:(id)theTable
{
	int selection = [_serverTable selectedRow];
	
	if (_delegate && [_delegate respondsToSelector:@selector(serverSelected:)])
	{
		[_delegate serverSelected:selection];
	}
}

- (BOOL)table:(id)theTable disclosureClickableForRow:(int)rowIndex
{
	return YES;
}

- (void)table:(UITable *)theTable disclosureClickedForRow:(int)row
{
	if (_delegate && [_delegate respondsToSelector:@selector(editServer:)])
	{
		[_delegate editServer:row];
	}
}

- (int)numberOfRowsInTable:(id)theTable
{
	return [_servers count];
}

- (id)table:(id)theTable cellForRow:(int)rowIndex column:(id)columnIndex
{	
	if (columnIndex == _serverLastConnectColumn )	
	{
		UIImageAndTextTableCell * cell = [[[UIImageAndTextTableCell alloc] init] autorelease];
		NSNumber *nb = [[_servers objectAtIndex:rowIndex] objectForKey:SERVER_LAST_CONNECT];
		if (nb == nil)
		{
			[cell setTitle:@"--- -- --:-- --"];
		}
		else
		{
			NSDate *dtLastConnect = [NSDate dateWithTimeIntervalSinceReferenceDate: [nb doubleValue]];
			[cell setTitle:[dtLastConnect descriptionWithCalendarFormat:@"%b %1d %I:%M %p" timeZone:nil locale:nil]];
		}
		
		[[cell titleTextLabel] setFont:_lastConnectFont];
		[[cell titleTextLabel] setColor:_textColorDateTime];

		[cell setShowDisclosure:YES];
		[cell setDisclosureClickable:YES];
		[cell setDisclosureStyle:1];
		return cell;
	}
	else
	{	
		UISimpleTableCell * cell = [[[UISimpleTableCell alloc] init] autorelease];
		[cell setTitle:[[_servers objectAtIndex:rowIndex] objectForKey:@"Name"]];
		[cell setShowDisclosure:NO];
		[cell setDisclosureClickable:YES];
		[cell setDisclosureStyle:1];
		return cell;
	}
	return nil;
}

- (BOOL)table:(id)theTable canSelectRow:(int)rowIndex;
{
	return YES;
}

/*
//These Methods track delegate calls made to the application
- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector 
{
	NSLog(@"Requested method for selector: %@", NSStringFromSelector(selector));
	return [super methodSignatureForSelector:selector];
}

- (BOOL)respondsToSelector:(SEL)aSelector 
{
	NSLog(@"Request for selector: %@", NSStringFromSelector(aSelector));
	return [super respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation 
{
	NSLog(@"Called from: %@", NSStringFromSelector([anInvocation selector]));
	[super forwardInvocation:anInvocation];
}
*/
@end
